/* 
模块的逻辑
*/
const md5 = require('md5')
const jwt = require('jsonwebtoken')
const {loginDao} = require('../dao//adminDao.js')
module.exports.loginService = async function(loginInfo){
  console.log(loginInfo,'业务逻辑层');
  //对密码加密
  loginInfo.loginPwd = md5(loginInfo.loginPwd)
  //接下来需要和数据库的密码比对
  const data  =await loginDao(loginInfo)
  if(data&&data.dataValues){
    // 添加token
    const dataValue = data.dataValues
    // 如果前端给过期时间就用没有就给默认值
    let loginPeriod = null
    loginInfo.remember ? loginPeriod = parseInt(loginInfo.remember):loginPeriod = 7
    const token = jwt.sign({
      id:dataValue.id,
      loginId:dataValue.loginId,
    },md5(process.env.JWT_SECRET),{expiresIn:60*60*loginPeriod})
    return  dataValue
    
  }
  return {data}
}